﻿using Factory.MasterData.Materials.Dto;
using Factory.MesEntity.CustomerDatas;
using Factory.WarehouseManagement.WarehouseSettings.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.WarehouseManagement.WarehouseSettings
{
    public class WarehouseSetAppService: ApplicationService, IWarehouseSetAppService
    {
        IRepository<WarehouseSet,int> _repository;

        public WarehouseSetAppService(IRepository<WarehouseSet,int> repository)
        {
            _repository = repository;
        }

        public async Task<PageResult> GetListAsync(string setName, int pageIndex = 1, int pageSize = 10)
        {
            var setlist=await _repository.GetListAsync();
            setlist= setlist.WhereIf(!string.IsNullOrEmpty(setName), x => x.SetName.Contains(setName)).ToList();
            PageResult pageResult = new PageResult();
            pageResult.RowCount = setlist.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = setlist.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }



        public int CreateAsync(WarehouseSet input)
        {
            var warehouseSetlist = _repository.GetListAsync(x => x.SetName == input.SetName).Result;

            // 检查是否已存在该单位名称
            if (warehouseSetlist.Count() > 0)
            {
                return -100;  // 表示重复，或者抛出异常等
            }

            var warehouselist = _repository.InsertAsync(new WarehouseSet
            {
                SetName = input.SetName,
                SetCode=input.SetCode,
                SetPeople=input.SetPeople,
                SetUnit=input.SetUnit,
                IsNot=input.IsNot,
                Result=input.Result,
                SetCapacity = input.SetCapacity,
            });
            return 1;
        }

        public async Task<int> UpdateAsync(WarehouseSetUpdateDTO input)
        {
            var warehouselist = await _repository.FindAsync(x => x.Id == input.Id);

            if (warehouselist == null)
            {
                // 客户未找到，可以根据需要抛出异常或返回特定值
                return 0;
            }

            warehouselist.SetName = input.SetName;
            warehouselist.SetCode = input.SetCode;
            warehouselist.SetPeople = input.SetPeople;
            warehouselist.SetUnit = input.SetUnit;
            warehouselist.IsNot = input.IsNot;
            warehouselist.Result = input.Result;
            warehouselist.SetCapacity = input.SetCapacity;
     

            await _repository.UpdateAsync(warehouselist);
            return 1; // 或者返回其他表示成功的值
        }

        public async Task<int> DeleteAsync(int id)
        {
            var warehouselist = await _repository.FindAsync(x => x.Id == id);
            if (warehouselist != null)
            {
                await _repository.DeleteAsync(warehouselist);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public WarehouseSet GetWarehouseSetById(int id)
        {
            var warehouselist = _repository.FindAsync(id).Result;
            return warehouselist;
        }
    }
}
